﻿//Orginal date made: 4th March 2012
//Creator: by Luke Bermingham

//Last modified:
//Modifier name:

using System;
using System.ComponentModel;

namespace SharedResources.ViewModel
{
    public abstract class BaseViewModel: INotifyPropertyChanged
    {
        //This is the base view model for all view models
        //it contains some generic behaviour that is useful in viewmodel
        //implementation.

        protected RelayCommand MakeCommandSafely(RelayCommand privateRelayCommand, Action commandLogic)
        {
            //This method saves froms having to write
            //if privateRelayCommand is null then make a real relay command everytime
            if(privateRelayCommand == null)
            {
                privateRelayCommand = new RelayCommand(ParamArrayAttribute => commandLogic());
            }
            return privateRelayCommand;
        }

        protected RelayCommand  MakeCommandSafely(RelayCommand privateRelayCommand, Action commandLogic, Func<bool> canExecuteLogic)
        {
            //Same as above except with canExcute param
            if (privateRelayCommand == null)
            {
                privateRelayCommand = new RelayCommand(ParamArrayAttribute => commandLogic(), ParamArrayAttribute => canExecuteLogic());
            }
            return privateRelayCommand;
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
